﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace DidaCode.Workflow
{
    public class NodeConfig
    {

        public static Dictionary<string, NodeConfig> RootConfig = new Dictionary<string, NodeConfig>();

        /// <summary>
        /// 开关配置，
        /// 1.true表示开启，false表示关闭
        /// 2.可查询配置中心获取决定是否开启
        /// 3.string类型空值表示开启
        /// </summary>
        public string SwitchConfig { get; set; }

        /// <summary>
        /// 节点失败后是否继续执行
        /// </summary>
        public bool ContinueOnFailure { get; set; }

        /// <summary>
        /// 子节点配置
        /// </summary>
        public Dictionary<string, NodeConfig> Children { get; set; }


        public static NodeConfig GetNodeConfig(string key)
        {
            if (!RootConfigDic.Any())
            {
                foreach (var childrenNodeConfig in RootConfig)
                {
                    SetNodeConfig(childrenNodeConfig.Key, childrenNodeConfig.Value);
                }
            }
            if (RootConfigDic.TryGetValue(key, out var nodeConfig))
            {
                return nodeConfig;
            }
            throw new Exception($"找不到{key}节点,请查看配置");
        }

        private static Dictionary<string, NodeConfig> RootConfigDic = new Dictionary<string, NodeConfig>();

        private static void SetNodeConfig(string key, NodeConfig nodeConfig)
        {

            RootConfigDic[key] = new NodeConfig { ContinueOnFailure = nodeConfig.ContinueOnFailure, SwitchConfig = nodeConfig.SwitchConfig };
            if (nodeConfig.Children != null)
            {
                foreach (var childrenNodeConfig in nodeConfig.Children)
                {
                    SetNodeConfig(childrenNodeConfig.Key, childrenNodeConfig.Value);
                }
            }
        }

    }
}
